package com.moyu.ui;

import com.moyu.activity.PanelGame;
import com.moyu.entity.People;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;

public class LeftLayTop extends Layout {
    PanelGame panelGame;
    public LeftLayTop(int x, int y, int w, int h, PanelGame panelGame) {
        super(x, y, w, h);
        this.panelGame = panelGame;
    }

    public void create(Graphics g) {
        this.createWindow(g);
        //西北农林科技大学
//        Image logo = new ImageIcon("resources/images/logo.png").getImage();
//        g.drawImage(logo, 42, 14, 231, 217, null);
        File file = new File("resources/data.txt");
        Image fontbg = new ImageIcon("resources/images/fontcolor.png").getImage();
        Image rankword = new ImageIcon("resources/images/rank.png").getImage();
        Font font = new Font("隶书", Font.BOLD, 20); // 创建字体对象
        g.drawImage(rankword,75,15,150,70,null);
        int x = 60; // 文本位置的横坐标
        int y = 92; // 文本位置的纵坐标

        // 设置字体
        g.setFont(font);
        ArrayList<People> al=new ArrayList();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                if (str.equals(""))
                    break;
                String[] strings = str.split(" ");
                al.add(new People(Integer.parseInt(strings[1]),strings[0]));
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //排序
        al.sort(People::compareTo);
        //限定只输出五行
        int count = al.size();
        if(count>5)
            count = 5;

        //设置字体颜色
        g.setColor(Color.cyan);
        //输出五行最高分
        for(int i=al.size()-1;i>=al.size()-count;i--) {
            String temp=al.get(i).toString();
            String [] NameAndPoint = temp.split(" ");
            g.drawImage(fontbg,x,y-20,185,24,null);
            g.drawString(NameAndPoint[0],x,y);
            g.drawString(String.format("%3s",NameAndPoint[1]),x+145,y);
            y+=30;
        }
    }
}